import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import { querySupplier, saveSupplier, editSupplier, delSupplier
  , queryCategory, addCategory, updateCategory, delCategory
  , queryGoods, delGoods, updateGoods, addGoods
  , inoutRecord, savebuyed
  , inoutRecordLogs, querywaring } from '@/services/inventory';

export default {
  namespace: 'Inventory',
  state: {
    supplierList: [],
    supplierLoading: true,
    category: {
      system: [],
      custom: [],
    },
    categoryLoading: true,
    warning: {
      list: [],
      isLoading: true,
    },
    goodsList: [],
    goodsLoading: true,
    total: 0,
    isLoading: true,
    ret: {},
    recordLogs: [],
    recordLogsLoading: true,
  },
  effects: {
    * querySupplier(payload, { call, put }) {
      yield put({
        type: 'updateSupplierLoading',
        loading: true
      })
      const response = yield call(querySupplier, payload.payload);
      yield put({
        type: 'updateSupplier',
        data: response,
      });
    },

    * saveSupplier(payload, {call, put}) {
      const {saveSupplierCallback} = payload.payload;
      const response = yield call(saveSupplier, payload.payload);
      return response;
    },

    * editSupplier(payload, {call, put}) {
      const {onEditSupplierCallback} = payload.payload;
      const response = yield call(editSupplier, payload.payload);
      return response;
    },

    * delSupplier(payload, {call, put}) {
      const response = yield call(delSupplier, payload.payload);
      return response;
    },

    * queryCategory(payload, { call, put }) {
      yield put({
        type: 'updateCategoryLoading',
        loading: true
      })
      const response = yield call(queryCategory, payload.payload);
      yield put({
        type: 'updateCategory',
        data: response,
        updateType: payload.payload.type
      });
    },

    * saveCategory(payload, {call, put}) {
      const response = yield call(addCategory, payload.payload);
      return response;
    }, 
    
    * editCategory(payload, {call, put}) {
      const response = yield call(updateCategory, payload.payload);
      return response;
    },

    * delCategory(payload, {call, put}) {
      const response = yield call(delCategory, payload.payload);
      return response;
    },

    * queryWarning(payload, { call, put }) {
      const response = yield call(querySupplier, payload.payload);
      yield put({
        type: 'updateSupplier',
        data: response,
      });
    },

    * queryGoods(payload, { call, put }) {
      yield put({
        type: 'updateGoodsLoading',
        loading: true
      })
      const response = yield call(queryGoods, payload.payload);
      yield put({
        type: 'updateGoods',
        data: response,
      });
    },

    * saveGoods(payload, {call, put}) {
      const response = yield call(addGoods, payload.payload);
      return response;
    }, 
    
    * editGoods(payload, {call, put}) {
      const response = yield call(updateGoods, payload.payload);
      return response;
    },

    * delGoods(payload, {call, put}) {
      const response = yield call(delGoods, payload.payload);
      return response;
    },

    * inoutRecord(payload, { call, put}) {
      const response = yield call(savebuyed, payload.payload1);
      const response2 = yield call(inoutRecord, payload.payload2);
      return response2
    },

    * queryRecordLogs(payload, { call, put }) {
      yield put({
        type: 'updateRecordLogsLoading',
        loading: true
      })
      const response = yield call(inoutRecordLogs, payload.payload);
      yield put({
        type: 'updateRecordsLogs',
        data: response,
      });
    },

    * querywaring(payload, { call, put }) {
      yield put({
        type: 'updateGoodsLoading',
        loading: true
      })
      const response = yield call(querywaring, payload.payload);
      yield put({
        type: 'updateGoods',
        data: response,
      });
    },

  },

  reducers: {
    updateSupplier(state, action) {
      const { code, ret } = action.data;
      return {
        ...state,
        supplierList: code === 1 ? ret : [],
        supplierLoading: false
      };
    },

    updateSupplierLoading(state, action) {
      return {
        ...state,
       supplierLoading: action.loading
      };
    },

    updateCategory(state, action) {
      const { code, ret } = action.data;
      const updateKey = action.updateType == '0' ? 'custom' : 'system'
      return {
        ...state,
        categoryLoading: false,
        category: {
          ...state.category,
          [updateKey]: code === 1 ? ret : [],
        },
      };
    },

    updateCategoryLoading(state, action){
      return {
        ...state,
        categoryLoading: action.loading
      }
    },

    updateGoods(state, action) {
      const { code, ret } = action.data;
      return {
        ...state,
        goodsLoading: false,
        goodsList: code === 1 ? ret : [],
      };
    },

    updateGoodsLoading(state, action){
      return {
        ...state,
        goodsLoading: action.loading
      }
    },

    updateRecordsLogs(state, action) {
      const { code, ret } = action.data;
      return {
        ...state,
        recordLogsLoading: false,
        recordLogs: code === 1 ? ret : [],
      };
    },

    updateRecordLogsLoading(state, action){
      return {
        ...state,
        recordLogsLoading: action.loading
      }
    },

  },
};


